import type {
  BaseEntity,
  ID,
  IDS,
  PageQuery,
  PageResult,
  Select,
} from '#/api/common';

import { requestClient } from '#/api/request';

/**
 * 租户
 */
export interface SysTenant extends BaseEntity {
  tenantId: ID;
  administrator: ID;
  tenantType: string;
  contactPhone: string;
  tenantName: string;
  status: number;
  domain: string;
  remark: string;
  packageId: string;
  expireTime: string;
  accountLimit: string;
  roleLimit: string;
  postLimit: string;
  deptLimit: string;
}

/**
 * 租户钱包
 */
export interface SysTenantWalletRecord extends BaseEntity {
  recordId: ID;
  tenantId: ID;
  amount: string;
  recordType: string;
  createAt: Date;
  remark: string;
}

const baseUrl = `/ur_biz/sys_tenant`;

// 查询租户列表
export function listTenant(query?: PageQuery) {
  return requestClient.get<PageResult<SysTenant>>(`${baseUrl}/list`, {
    params: query,
  });
}

/**
 * 租户选择列表, 带分页
 */
export function listSelectTenant(query?: any) {
  return requestClient.get<PageResult<Select>>(`${baseUrl}/selectlist`, {
    params: query,
  });
}

// 查询租户套餐选择列表
export function packageSelect(packageName?: string) {
  return requestClient.get<Select[]>(`${baseUrl}/package_select`, {
    params: packageName,
  });
}

// 查询租户详细
export function getTenant(tenantId: ID) {
  return requestClient.get<SysTenant>(`${baseUrl}/${tenantId}`);
}

// 根据租户ID查询钱包记录
export function listWalletRecord(query: PageQuery) {
  return requestClient.get<PageResult<SysTenantWalletRecord>>(
    `${baseUrl}/wallet_record/list`,
    {
      params: query,
    },
  );
}

// 重置租户密码
export function resetOperPwd(id: ID, password: string) {
  return requestClient.post(`${baseUrl}/reset_operpwd`, { id, password });
}

// 新增租户
export function addTenant(data: any) {
  return requestClient.post(`${baseUrl}/add`, data);
}

// 修改租户
export function editTenant(data: any) {
  return requestClient.post(`${baseUrl}/edit`, data);
}

// 修改租户状态
export function changeStatus(id: ID, status: string) {
  return requestClient.post(`${baseUrl}/edit_status`, {
    id,
    status,
  });
}

// 删除租户
export function delTenant(tenantIds: IDS) {
  return requestClient.post(`${baseUrl}/remove`, tenantIds);
}
